package servlet;

import hibernate.Role;
import hibernate.User;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.RoleDAO;

/**
 * Servlet implementation class SearchJS
 */
public class SearchJS extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchJS() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    public Vector showRole(Role r,int condition){
    	Vector v = new Vector();
    	Vector vselect = new Vector();
    	Vector vunselect = new Vector();
    	
		v.add(r.getrAssetCreate());
		v.add(r.getrAssetRead());
		v.add(r.getrAssetUpdate());
		v.add(r.getrAssetDelete());
		
		v.add(r.getrAssetDamageCreate());
		v.add(r.getrAssetDamageRead());
		v.add(r.getrAssetDamageUpdate());
		v.add(r.getrAssetDamageDelete());
		
		v.add(r.getrAssetChangeCreate());
		v.add(r.getrAssetChangeRead());
		v.add(r.getrAssetChangeUpdate());
		v.add(r.getrAssetChangeDelete());
		
		v.add(r.getrAssetSaleCreate());
		v.add(r.getrAssetSaleRead());
		v.add(r.getrAssetSaleUpdate());
		v.add(r.getrAssetSaleDelete());
		
		v.add(r.getrAssetServiceCreate());
		v.add(r.getrAssetServiceRead());
		v.add(r.getrAssetServiceUpdate());
		v.add(r.getrAssetServiceDelete());

		v.add(r.getrPurchaseSupplyCreate());
		v.add(r.getrPurchaseSupplyRead());
		v.add(r.getrPurchaseSupplyUpdate());
		v.add(r.getrPurchaseSupplyDelete());
    	v.add(r.getrPurchaseSupplyApprove());
		
    	v.add(r.getrOldReport());
    	v.add(r.getrChangeReport());
    	v.add(r.getrServiceReport());
    	v.add(r.getrAssetReport());

    	v.add(r.getrCreateUser());
    	v.add(r.getrReadUser());
    	v.add(r.getrUpdateUser());
    	v.add(r.getrDeleteUser());
		
    	v.add(r.getrCreateRole());
    	v.add(r.getrReadRole());
    	v.add(r.getrUpdateRole());
    	v.add(r.getrDeleteRole());
		
    	v.add(r.getrAssetSortDictionary());
    	v.add(r.getrDepartmentDictionary());
    	
    	for(int i=0;i<v.size();i++){
    		if(((Integer)v.get(i))==0){
    			vunselect.add(i+1);
    		}else{
    			vselect.add(i+1);
    		}
    	}
    	if(condition == 0){
    		return vselect;
    	}else if(condition == 1){
    		return vunselect;
    	}else
    		return v;
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String un="";
	    request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");		
		String rolename = request.getParameter("roleName");
		un = request.getParameter("un");
		
		System.out.println(".............................."+un);
		
		RoleDAO rd = new RoleDAO();
		Vector v = new Vector();
		Vector v1 =new Vector();
	    
		String []JSMC = {"资产维护添加","资产维护删除","资产维护更新","资产维护查询","资产报损添加","资产报损删除","资产报损更新","资产报损查询",
				         "资产调拨添加","资产调拨删除","资产调拨更新","资产调拨查询","资产出售添加","资产出售删除","资产出售更新","资产出售查询",
				         "资产维修添加","资产维修删除","资产维修更新","资产维修查询","资产报表查看添加","资产报表查看删除","资产报表查看更新",
				         "资产报表查看查询","购置申请添加","购置申请删除","购置申请更新","购置申请查询","购置申请审批",
				         "用户添加","用户删除","用户更新","用户查询","角色添加","角色删除","角色更新","角色查询","添加部门","添加资产类别"}; 
	    if("".equals(rolename))
	    {
	    	v = rd.getAll(start, limit);	
	    }
	    else
	    {
	    	v = rd.searchUser(start, limit, rolename);
	    }
	    
	    String count = (v.elementAt(0)).toString();
	    @SuppressWarnings("unchecked")
	    List list = (List)v.elementAt(1);
	    
		
		int SizeOfList=list.size();
		Role r;
		String s = "";
		int num;
		if(un.equals("")){
			s = "{totalCount:" + count + "," + "result:[";
			for(int i=0;i<SizeOfList;i++)
			{   
				r = ((Role)list.get(i));
				v1 = showRole(r,-1);
				//s += "{roleID:" +"'"+  user.getUserID() + "',";
				s +="{roleID:" +"'"+ r.getRoleID() + "',";
				s += "name:" +"'"+  r.getName() + "',";
				s += "rdescribe:" +"'"+ r.getRdescribe() + "',";
				s += "rAssetCreate:" +"'"+  (Integer)v1.get(0) + "',";
				s += "rAssetRead:" +"'"+  (Integer)v1.get(1) + "',";
				s += "rAssetUpdate:" +"'"+  (Integer)v1.get(2) + "',";
				s += "rAssetDelete:" +"'"+  (Integer)v1.get(3) + "',";
				
				s += "rAssetDamageCreate:" +"'"+  (Integer)v1.get(4) + "',";
				s += "rAssetDamageRead:" +"'"+  (Integer)v1.get(5) + "',";
				s += "rAssetDamageUpdate:" +"'"+  (Integer)v1.get(6) + "',";
				s += "rAssetDamageDelete:" +"'"+  (Integer)v1.get(7) + "',";
				
				s += "rAssetChangeCreate:" +"'"+  (Integer)v1.get(8) + "',";
				s += "rAssetChangeRead:" +"'"+  (Integer)v1.get(9) + "',";
				s += "rAssetChangeUpdate:" +"'"+  (Integer)v1.get(10) + "',";
				s += "rAssetChangeDelete:" +"'"+  (Integer)v1.get(11) + "',";
				
				s += "rAssetSaleCreate:" +"'"+  (Integer)v1.get(12) + "',";
				s += "rAssetSaleRead:" +"'"+  (Integer)v1.get(13) + "',";
				s += "rAssetSaleUpdate:" +"'"+  (Integer)v1.get(14) + "',";
				s += "rAssetSaleDelete:" +"'"+  (Integer)v1.get(15) + "',";
				
				s += "rAssetServiceCreate:" +"'"+  (Integer)v1.get(16) + "',";
				s += "rAssetServiceRead:" +"'"+  (Integer)v1.get(17) + "',";
				s += "rAssetServiceUpdate:" +"'"+  (Integer)v1.get(18) + "',";
				s += "rAssetServiceDelete:" +"'"+  (Integer)v1.get(19) + "',";
	
				s += "rPurchaseSupplyCreate:" +"'"+  (Integer)v1.get(20) + "',";
				s += "rPurchaseSupplyRead:" +"'"+  (Integer)v1.get(21) + "',";
				s += "rPurchaseSupplyUpdate:" +"'"+  (Integer)v1.get(22) + "',";
				s += "rPurchaseSupplyDelete:" +"'"+  (Integer)v1.get(23) + "',";	
				s += "rPurchaseSupplyApprove:" +"'"+  (Integer)v1.get(24) + "',";
				
				s += "rOldReport:" +"'"+  (Integer)v1.get(25) + "',";
				s += "rChangeReport:" +"'"+  (Integer)v1.get(26) + "',";
				s += "rServiceReport:" +"'"+  (Integer)v1.get(27) + "',";
				s += "rAssetReport:" +"'"+  (Integer)v1.get(28) + "',";
	
				s += "rCreateUser:" +"'"+  (Integer)v1.get(29) + "',";
				s += "rReadUser:" +"'"+  (Integer)v1.get(30) + "',";
				s += "rUpdateUser:" +"'"+  (Integer)v1.get(31) + "',";
				s += "rDeleteUser:" +"'"+  (Integer)v1.get(32) + "',";
				
				s += "rCreateRole:" +"'"+  (Integer)v1.get(33) + "',";
				s += "rReadRole:" +"'"+  (Integer)v1.get(34) + "',";
				s += "rUpdateRole:" +"'"+  (Integer)v1.get(35) + "',";
				s += "rDeleteRole:" +"'"+  (Integer)v1.get(36) + "',";
				
				s += "rAssetSortDictionary:" +"'"+  (Integer)v1.get(37) + "',";
				s += "rDepartmentDictionary:" +"'"+  (Integer)v1.get(38) + "',}";	
				if(i!=SizeOfList-1)
					s += ",";
			}
		}else{
			s += "{result:[";
			for(int i=0;i<SizeOfList;i++){
				r = ((Role)list.get(i));
				v1 = showRole(r,Integer.parseInt(un));
				for(int j=0;j<v1.size();j++){
					num = (Integer)v1.get(j);
					s +="{value:"+ num +",text:'"+ JSMC[num-1] +"'}";
					if(j != v1.size()-1)
						s += ",";
				}
			}
		}
		s = s + "]}";
        out.write(s);
        System.out.println(s);
        out.flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
